<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * @package       Pfw
 * @author        Sean Sitter <sean@picnicphp.com>
 * @copyright     2010 The Picnic PHP Framework
 * @license       http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 * @link          http://www.picnicphp.com
 * @since         0.10
 * @filesource
 */

Pfw_Loader::loadClass("Pfw_Paginator");

class Pfw_Paginator_SelectBuilder extends Pfw_Paginator
{
    protected $select;

    /**
     * @param Pfw_Db_Select $select
     * @param int $page_num
     * @param int $per_page
     */
    public function  __construct($select, $page_num = 1, $per_page = self::DEFAULT_PER_PAGE)
    {
        parent::__construct($page_num, $per_page);
        $this->select = clone $select;
    }

    public function paginate()
    {
        if (!is_null($this->collection)) {
            return $this->collection;
        }
        $this->select->offset($this->getOffset());
        $this->select->limit($this->getPerPage());
        $this->collection = $this->select->exec();
        return $this->collection;
    }

    public function getTotal()
    {
        if (!is_null($this->total)) {
            return $this->total;
        }
        $this->total = $this->select->count();
        return $this->total;
    }
}

?>